#include <nice2d/string.h>
#include <nice2d/piece/factionmanager.h>

class FactionManagerData
{
public:
    std::vector<std::shared_ptr<Faction>> factions;
    uint32_t current = 0;
};

FactionManager::FactionManager():
    data(new FactionManagerData())
{
}

FactionManager::~FactionManager()
{
}

FactionPointer FactionManager::createFaction(const sf::Color &color,const std::wstring &name, const std::wstring &desc)
{
    auto faction = std::make_shared<Faction>(color, name, desc);
    data->factions.push_back(faction);
    return faction;
}

void FactionManager::switchFaction()
{
    if (data->factions.empty()) {
        data->current = -1;
        return;
    }

    data->current ++;
    if (data->current >= data->factions.size())
        data->current = 0;
}

FactionPointer FactionManager::getCurrentFaction()
{
    if (data->current < 0 || data->current >= data->factions.size())
        return nullptr;
    return data->factions[data->current];
}

void FactionManager::update(float deltaTime)
{
}

PTree FactionManager::serialize()
{
    PTree node;
    //TODO
    return node;
}

void FactionManager::deserialize(const PTree &node)
{
    //TODO
}

FactionPointer FactionManager::getFaction(const sf::Color &color)
{
    auto itr = data->factions.begin();
    while (itr != data->factions.end()) {
        if ((*itr)->getColor() == color)
            return *itr;
        itr ++;
    }
    return nullptr;
}
